<?php 
//检测应用是否需要更新
header("content-type:text/html;charset=utf8");
$return = array();
if (isset($_GET['versionCode'])) {
	$pdo = new PDO('mysql:host=123.57.16.97;dbname=project','root','zqVnUy6cxsQXbpEZ');
	$pdo->exec("set names utf8");
	//获取最新信息
	$info = getUpdateInfo($pdo);
	//判断是否需要更新 并且返回实际需要更新内容
	$return = judgementUpdate($info,$_GET['versionCode'],$pdo);
}else{
	$return['code'] = 10001;
	$return['data'] = 'Lack of necessary parameters';
}

echo json_encode($return);

//获取数据库最新信息
function getUpdateInfo($pdo){
	$sql = "SELECT versionCode,versionName,download FROM app_update";
	$info = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
	return $info;
}

//判断更新情况
function judgementUpdate($info,$versionCode,$pdo){
	$updateInfo = array();
	if ($info['versionCode'] > $versionCode) {
		$sql = 'SELECT `explain` FROM app_update_explain';
		$explain = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		$updateInfo['code'] = 10000;
		$updateInfo['data'] = dataHandle($info,$explain);
	}else{
		$updateInfo['code'] = 10002;
		$updateInfo['data'] = 'Currently for the latest version, there is no need to do more';
	}
	return $updateInfo;
}

//处理数据
function dataHandle($info,$explain){
	foreach ($explain as $k => $v) {
		$info['explain'][] = $v['explain'];
	}
	return $info;
}
 ?>